/*************************************************************************************
 **																					**
 **	interrupt_handlers.c															**
 ** 																				**
 **************************************************************************************
 **																					**
 ** Written By:	Steve Montgomery													**
 **				Digital Six Laboratories LLC										**
 ** (c)2012,2013,2014 Digital Six Labs, All rights reserved							**
 **																					**
 **************************************************************************************/
//
// Revision History
//
// Revision		Date	Reviser		Description
// ===================================================================================
// ===================================================================================

#include "iodefine.h"
#include "iodefine_ext.h"
#include "interrupt_handlers.h"

/*
 * INT_WDTI (0x4)
 */
void INT_WDTI (void) { }

/*
 * INT_LVI (0x6)
 */
void INT_LVI (void) { }

/*
 * INT_P0 (0x8)
 */
#ifdef IAR
#pragma vector = INTP0_vect
__interrupt void INT_P0(void)
{
  HandleInterrupt(0);
}
#else
void INT_P0 (void)
{
	HandleInterrupt(0);
}
#endif
/*
 * INT_P1 (0xA)
 */
#ifdef IAR
#pragma vector = INTP1_vect
__interrupt void INT_P1(void)
{
  HandleInterrupt(1);
}
#else
void INT_P1 (void)
{
	HandleInterrupt(1);
}
#endif
/*
 * INT_P2 (0xC)
 */
void INT_P2 (void)
{
	HandleInterrupt(2);
}

/*
 * INT_P3 (0xE)
 */
void INT_P3 (void)
{
	HandleInterrupt(3);
}

/*
 * INT_P4 (0x10)
 */
void INT_P4 (void)
{
	HandleInterrupt(4);
}

/*
 * INT_P5 (0x12)
 */
void INT_P5 (void)
{
	HandleInterrupt(5);
}

/*
 * INT_CSI20/INT_IIC20/INT_ST2 (0x14)
 */
void INT_ST2 (void) { }
//void INT_CSI20 (void) { }
//void INT_IIC20 (void) { }

/*
 * INT_CSI21/INT_IIC21/INT_SR2 (0x16)
 */
void INT_SR2 (void) { }
//void INT_CSI21 (void) { }
//void INT_IIC21 (void) { }

/*
 * INT_SRE2 (0x18)
 */
void INT_SRE2 (void) { }

/*
 * INT_DMA0 (0x1A)
 */
void INT_DMA0 (void) { }

/*
 * INT_DMA1 (0x1C)
 */
void INT_DMA1 (void) { }

/*
 * INT_CSI00/INT_IIC00/INT_ST0 (0x1E)
 */
#ifdef UART_ENABLED
U8 _uart0TransmitBuffer[UARTBUFFERSIZE];
U8 _uart0TransmitTailPointer = 0;
#endif
#ifdef IAR
#pragma vector = INTST0_vect 
__interrupt void INT_UART0_TX (void)
{

#ifdef UART_ENABLED
	TXD0 = _uart0TransmitBuffer[_uart0TransmitTailPointer++];
	_uart0TransmitTailPointer&=UARTBUFFERSIZE-1;
#endif
}
#else
void INT_UART0_TX (void)
{

#ifdef UART_ENABLED
	TXD0 = _uart0TransmitBuffer[_uart0TransmitTailPointer++];
	_uart0TransmitTailPointer&=UARTBUFFERSIZE-1;
#endif
}
#endif
//void INT_CSI00 (void) { }
//void INT_IIC00 (void) { }

/*
 * UART0 Receive
 */

#ifdef UART_ENABLED
U8 _uart0ReceiveBuffer[UARTBUFFERSIZE];
U8 _uart0ReceiveTailPointer = 0;
#endif
#ifdef IAR
#pragma vector = INTSR0_vect
__interrupt void INT_UART0_RX (void)
{

#ifdef UART_ENABLED
	_uart0ReceiveBuffer[_uart0ReceiveTailPointer++] = RXD0;
	_uart0ReceiveTailPointer&=UARTBUFFERSIZE-1;
#endif
}
#else
void INT_UART0_RX (void)
{

#ifdef UART_ENABLED
	_uart0ReceiveBuffer[_uart0ReceiveTailPointer++] = RXD0;
	_uart0ReceiveTailPointer&=UARTBUFFERSIZE-1;
#endif
}
#endif

/*
 * INT_SRE0/INT_TM01H (0x22)
 */
void INT_TM01H (void) { }
//void INT_SRE0 (void) { }

/*
 * INT_ST1 (0x24)
 */

#ifdef UART_ENABLED
U8 _uart1TransmitBuffer[UARTBUFFERSIZE];
U8 _uart1TransmitTailPointer = 0;

extern U8 _uart1TransmitHeadPointer;
#endif
#ifdef IAR
#pragma vector = INTST1_vect
__interrupt void INT_UART1_TX (void)
 {
#ifdef UART_ENABLED
	 _uart1TransmitTailPointer++;
	 _uart1TransmitTailPointer&=UARTBUFFERSIZE-1;

	 if(_uart1TransmitTailPointer != _uart1TransmitHeadPointer)
		 TXD1 = _uart1TransmitBuffer[_uart1TransmitTailPointer];
#endif
 }
#else
 void INT_UART1_TX (void)
 {
#ifdef UART_ENABLED
	 _uart1TransmitTailPointer++;
	 _uart1TransmitTailPointer&=UARTBUFFERSIZE-1;

	 if(_uart1TransmitTailPointer != _uart1TransmitHeadPointer)
		 TXD1 = _uart1TransmitBuffer[_uart1TransmitTailPointer];
#endif
 }
#endif

/*
 * UART1 Receive
 */
#ifdef UART_ENABLED
U8 _uart1ReceiveBuffer[UARTBUFFERSIZE];
U8 _uart1ReceiveTailPointer = 0;
#endif
#ifdef IAR
#pragma vector = INTSR1_vect
__interrupt void INT_UART1_RX (void)
{
#ifdef UART_ENABLED
	U8 val = RXD1;

	// TODO: Debug this and make sure that it works properly
	// if we have no errors, process the byte


		_uart1ReceiveBuffer[_uart1ReceiveTailPointer++] = RXD1;
		_uart1ReceiveTailPointer&=UARTBUFFERSIZE-1;

	// TODO:  Make sure that this allows the serial port to function on error.  Right now, it hangs.  We added this line to fix that.
	// make sure we clear any error flags lest we shut down.
	SIR03 = _0004_SAU_SIRMN_FECTMN | _0002_SAU_SIRMN_PECTMN | _0001_SAU_SIRMN_OVCTMN;

#endif
}
#else
void INT_UART1_RX (void)
{
#ifdef UART_ENABLED
	U8 val = RXD1;

	// TODO: Debug this and make sure that it works properly
	// if we have no errors, process the byte


		_uart1ReceiveBuffer[_uart1ReceiveTailPointer++] = RXD1;
		_uart1ReceiveTailPointer&=UARTBUFFERSIZE-1;

	// TODO:  Make sure that this allows the serial port to function on error.  Right now, it hangs.  We added this line to fix that.
	// make sure we clear any error flags lest we shut down.
	SIR03 = _0004_SAU_SIRMN_FECTMN | _0002_SAU_SIRMN_PECTMN | _0001_SAU_SIRMN_OVCTMN;

#endif
}
#endif
//void INT_CSI11 (void) { }
//void INT_IIC11 (void) { }

/*
 * INT_SRE1/INT_TM03H (0x28)
 */
#ifdef IAR
#pragma vector = INTSRE1_vect
__interrupt void INT_SRE1 (void)
{
	U8 dat = RXD1;
	SIR03|=0x07;
	ST0 |= _0008_SAU_CH3_STOP_TRG_ON;
	SS0 |= _0008_SAU_CH3_STOP_TRG_ON;;
}

#else
void INT_SRE1 (void)
{
	U8 dat = RXD1;
	SIR03|=0x07;
	ST0 |= _0008_SAU_CH3_STOP_TRG_ON;
	SS0 |= _0008_SAU_CH3_STOP_TRG_ON;;
}
#endif
//void INT_SRE1 (void) { }

/*
 * INT_IICA0 (0x2A)
 */
void INT_IICA0 (void) { }

/*
 * INT_TM00 (0x2C)
 */
void INT_TM00 (void) { }

/*
 * INT_TM01 (0x2E)
 */
void INT_TM01 (void) { }

/*
 * INT_TM02 (0x30)
 */
void INT_TM02 (void) { }

/*
 * INT_TM03 (0x32)
 */
void INT_TM03 (void) { }

/*
 * INT_AD (0x34)
 */
void INT_AD (void) { }

/*
 * INT_RTC (0x36)
 */
// this interrupt should fire every 1 seconds
U8 _RTCSeconds = 0;
// 24 hours counter
U8 _RTCHours = 0;
U8 _RTCMinutes = 0;
// 0=Sunday, 6=Saturday
U8 _RTCDayOfWeek = 0;
// counts date time in seconds since 00:00:00 1/1/2013.  Give us 136 years dynamic range.  13,735,245 = 23:20:45 6/7/2013
UU32 _RTCDateTimeInSecs;
#ifdef IAR
#pragma vector = INTRTC_vect
__interrupt void INT_RTC (void)
{
	if(RIFG==1)
	{
		RTCC1 &= ~0x08;
		Handle1SecInterrupt();
		_RTCSeconds++;
		_RTCDateTimeInSecs.U32++;
		if(_RTCSeconds>59)
		{
			_RTCSeconds=0;
			_RTCMinutes++;
			if(_RTCMinutes>59)
			{
				_RTCMinutes = 0;
				_RTCHours++;
				if(_RTCHours>23)
				{
					_RTCHours=0;
					_RTCDayOfWeek++;
					if(_RTCDayOfWeek>6)
						_RTCDayOfWeek=0;
				}
			}
		}
	}
}
#else
void INT_RTC (void)
{
	if(RIFG==1)
	{
		RTCC1 &= ~0x08;
		Handle1SecInterrupt();
		_RTCSeconds++;
		_RTCDateTimeInSecs.U32++;
		if(_RTCSeconds>59)
		{
			_RTCSeconds=0;
			_RTCMinutes++;
			if(_RTCMinutes>59)
			{
				_RTCMinutes = 0;
				_RTCHours++;
				if(_RTCHours>23)
				{
					_RTCHours=0;
					_RTCDayOfWeek++;
					if(_RTCDayOfWeek>6)
						_RTCDayOfWeek=0;
				}
			}
		}
	}
}
#endif
/*
 * INT_IT (0x38)
 */
#ifdef IAR
#pragma vector = INTIT_vect
__interrupt 
void INT_IT (void)
{
	Handle1MsInterrupt();
}
#else
void INT_IT (void)
{
	Handle1MsInterrupt();
}
#endif
/*
 * INT_KR (0x3A)
 */
void INT_KR (void)
{

	HandleInterrupt(0x80 + (P7&0x07));
}

/*
 * INT_TM04 (0x42)
 */
void INT_TM04 (void) { }

/*
 * INT_TM05 (0x44)
 */
void INT_TM05 (void) { }

/*
 * INT_TM06 (0x46)
 */
void INT_TM06 (void) { }

/*
 * INT_TM07 (0x48)
 */
void INT_TM07 (void) { }

/*
 * INT_MD (0x5E)
 */
void INT_MD (void) { }

/*
 * INT_FL (0x62)
 */
void INT_FL (void) { }

/*
 * INT_BRK_I (0x7E)
 */
void INT_BRK_I (void) { }
